package com.atguigu.ai.functioncall.controller;

import com.atguigu.ai.functioncall.service.CalculatorService;
import com.atguigu.ai.functioncall.service.TimeService;
import com.atguigu.ai.functioncall.service.WeatherService;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.Map;

@RestController
@RequestMapping("/function")
public class FunctionCallController {

    @Autowired
    private ChatClient chatClient;

    @Autowired
    private WeatherService weatherService;

    @Autowired
    private CalculatorService calculatorService;

    @Autowired
    private TimeService timeService;

    /**
     * 天气查询函数调用
     */
    @GetMapping("/weather")
    public String weatherFunction(@RequestParam(value = "msg", defaultValue = "北京今天天气怎么样？") String message) {
        return chatClient.prompt()
                .user(message)
                .functions("weatherService")  // 注册函数
                .call()
                .content();
    }

    /**
     * 计算器函数调用
     */
    @GetMapping("/calculator")
    public String calculatorFunction(@RequestParam(value = "msg", defaultValue = "计算 15 + 25 等于多少？") String message) {
        return chatClient.prompt()
                .user(message)
                .functions("calculatorService")
                .call()
                .content();
    }

    /**
     * 时间查询函数调用
     */
    @GetMapping("/time")
    public String timeFunction(@RequestParam(value = "msg", defaultValue = "现在几点了？") String message) {
        return chatClient.prompt()
                .user(message)
                .functions("timeService")
                .call()
                .content();
    }

    /**
     * 多函数调用 - 注册多个函数
     */
    @GetMapping("/multi")
    public String multiFunctions(@RequestParam(value = "msg", defaultValue = "帮我查一下北京的天气，然后计算一下当前时间加上3小时是几点") String message) {
        return chatClient.prompt()
                .user(message)
                .functions("weatherService", "calculatorService", "timeService")
                .call()
                .content();
    }

    /**
     * 流式函数调用
     */
    @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> streamFunction(@RequestParam(value = "msg", defaultValue = "请查询上海的天气情况，并计算一下25乘以4等于多少") String message) {
        return chatClient.prompt()
                .user(message)
                .functions("weatherService", "calculatorService")
                .stream()
                .content();
    }

    /**
     * 复杂场景函数调用
     */
    @PostMapping("/complex")
    public String complexFunction(@RequestBody ComplexRequest request) {
        String systemPrompt = """
                你是一个智能助手，可以帮助用户查询天气、进行数学计算和获取时间信息。
                当用户询问相关问题时，请调用相应的函数来获取准确的信息。
                请用友好和专业的语调回答用户的问题。
                """;

        return chatClient.prompt()
                .system(systemPrompt)
                .user(request.getMessage())
                .functions("weatherService", "calculatorService", "timeService")
                .call()
                .content();
    }

    /**
     * 获取可用函数列表
     */
    @GetMapping("/functions")
    public Map<String, Object> getFunctions() {
        return Map.of(
                "weatherService", Map.of(
                        "description", "天气查询服务",
                        "parameters", "city: 城市名称",
                        "example", "/function/weather?msg=北京今天天气怎么样？"
                ),
                "calculatorService", Map.of(
                        "description", "数学计算服务",
                        "parameters", "operation: 运算类型, a: 第一个数字, b: 第二个数字",
                        "example", "/function/calculator?msg=计算 15 + 25"
                ),
                "timeService", Map.of(
                        "description", "时间查询服务",
                        "parameters", "timezone: 时区, format: 时间格式",
                        "example", "/function/time?msg=现在几点了？"
                ),
                "multiFunctions", Map.of(
                        "description", "多函数调用示例",
                        "example", "/function/multi?msg=查询北京天气并计算当前时间"
                ),
                "streamFunction", Map.of(
                        "description", "流式函数调用",
                        "example", "/function/stream?msg=查询天气并进行计算"
                )
        );
    }

    /**
     * 函数调用帮助信息
     */
    @GetMapping("/help")
    public Map<String, Object> getHelp() {
        return Map.of(
                "说明", "Spring AI 函数调用演示",
                "功能", Map.of(
                        "天气查询", "可以查询指定城市的天气情况",
                        "数学计算", "支持加减乘除、幂运算、取模等数学运算",
                        "时间查询", "可以获取指定时区的当前时间"
                ),
                "使用方法", Map.of(
                        "单一函数", "使用 /function/{functionName} 调用特定函数",
                        "多函数", "使用 /function/multi 同时使用多个函数",
                        "流式调用", "使用 /function/stream 进行流式函数调用",
                        "复杂场景", "使用 POST /function/complex 进行复杂的函数调用"
                ),
                "示例", Map.of(
                        "天气查询", "北京今天天气怎么样？",
                        "数学计算", "计算 15 + 25 等于多少？",
                        "时间查询", "现在几点了？",
                        "综合查询", "查询上海天气，然后计算当前时间加3小时"
                )
        );
    }

    // 复杂请求的数据类
    public static class ComplexRequest {
        private String message;

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }
} 